package ch.ethz.fcl.metrobuzz.render.techniques.circos;

import java.awt.Color;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import ch.ethz.fcl.metrobuzz.MBContainer;
import ch.ethz.fcl.metrobuzz.controller.MBController;
import ch.ethz.fcl.metrobuzz.render.techniques.circos.version2.CircosDiagram2;
import ch.ethz.fcl.metrobuzz.render.techniques.circos.version5.CircosDiagram5;
import ch.ethz.fcl.metrobuzz.render.techniques.colors.MBColorTable;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;
import ch.ethz.fcl.metrobuzz.tools.MBGL;
import ch.ethz.fcl.metrobuzz.tools.MBTools;
import ch.ethz.fcl.metrobuzz.viewer.MBViewer;
import ch.ethz.fcl.metrofont.MetroFont;

public class CircosTest {
	CircosMatrix matrix;
	CircosDiagram2 viewer;
	MetroFont fm;

	private MBViewer view;
	private MBController control;

	public CircosTest() {
		init();
	}

	public void run() {
		while (true) {
			GL11.glPushMatrix();

			view.update();
			render();
			control.update();

			GL11.glPopMatrix();
			Display.sync(30);
			Display.update();
		}
	}

	private void init() {
		MBContainer container = new MBContainer();

		int width = 800;
		int height = 600;

		view = new MBViewer(MBViewer.ProjectionType.Perspective, container);
		view.createDisplay(width, height, width, height, "Circos Test");
		view.getCamera().disablePan();
		view.getCamera().initDistance(2.5f);

		control = new MBController(container);

		int size = 5;
		matrix = new CircosMatrix(size);
		for (int i = 0; i < size; i++)
			for (int j = 0; j < size; j++) {
				matrix.setData(i, j, (float) (Math.random() * 100));
				if (i > 0)
					matrix.setColor(i - 1, (i - 1) * 3 % 10);
			}
		// data.getDirections().setDirection(4, new Vector2d(0.1, -0.9));
		//
		// data.setData(0, 1, 0);
		// data.setData(1, 0, 100);

		// data.setInitDirs(1, MBTools.rotate(xDir, Math.PI / 3));

		matrix.setData(0, 1, 14);
		matrix.setData(0, 2, 14);
		matrix.setData(0, 3, 50);
		matrix.setData(0, 4, 81);

		matrix.setData(1, 0, 32);
		matrix.setData(1, 2, 22);
		matrix.setData(1, 3, 140);
		matrix.setData(1, 4, 20);

		matrix.setData(2, 0, 12);
		matrix.setData(2, 1, 84);
		matrix.setData(2, 3, 14);
		matrix.setData(2, 4, 25);

		matrix.setData(3, 0, 56);
		matrix.setData(3, 1, 40);
		matrix.setData(3, 2, 57);
		matrix.setData(3, 4, 14);

		matrix.setData(4, 0, 25);
		matrix.setData(4, 1, 20);
		matrix.setData(4, 2, 10);
		matrix.setData(4, 3, 181);

		matrix.setColor(0, MBColorTable.LIGHTGREY);

		fm = new MetroFont();
		fm.setFontColor(Color.black);
		// fm.setFontColor(Color.BLUE);
		// fm.setFontName("Zapfino");

		MBTools.debug(matrix.toString(), false);
		viewer = new CircosDiagram5(new Point2D(0, 0), 0.5f, matrix);
	}

	private void render() {
		GL11.glClearColor(1, 1, 1, 1);
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		MBGL.enableBlend();
		viewer.render(fm);
		MBGL.disableBlend();
	}

	public static void main(String[] args) {
		new CircosTest().run();
	}
}